"""
Case Type   : 系统内部使用工具
Case Name   : 指定--includedir-server参数打印那些做服务器编程时使用
              的C头文件的位置是否成功
Create At   : 2020/9/16
Owner       : opentestcase003
Description :
    1.获取GAUSSHOME路径
    2.执行命令打印那些做服务器编程时使用的C头文件的位置
Expect      :
    1.获取GAUSSHOME路径成功
    2.执行命令打印那些做服务器编程时使用的C头文件的位置
History     :
    @modified: by @lihongji 2025/1/21 优化GAUSSHOME路径获取方式
"""

import unittest
import os

from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()


class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        LOG.info('-------------------this is setup--------------------')
        LOG.info('---Opengauss_Function_Tools_pg_config_Case0006开始执行---')
        self.PrimaryNode = Node('PrimaryDbUser')

    def test_system_internal_tools(self):
        text = '----step1:获取GAUSSHOME路径  expect:路径获取成功-----'
        LOG.info(text)
        excute_cmd1 = f' cat {macro.DB_ENV_PATH} | grep GAUSSHOME='
        LOG.info(excute_cmd1)
        msg1 = self.PrimaryNode.sh(excute_cmd1).result()
        self.gaussHome_path = msg1.split('=')[-1]
        LOG.info(self.gaussHome_path)
        self.assertIn('app', self.gaussHome_path, '执行失败:' + text)

        text = '----step2:打印做服务器编程时使用的C头文件  expect:成功------'
        LOG.info(text)
        print_cmd = f'''source {macro.DB_ENV_PATH};
            pg_config --includedir-server;'''
        LOG.info(print_cmd)
        print_msg = self.PrimaryNode.sh(print_cmd).result()
        LOG.info(print_msg)
        path = os.path.join(self.gaussHome_path, 'include', 'postgresql', 'server')
        self.assertIn(path, print_msg, '执行失败:' + text)

    def tearDown(self):
        LOG.info('-----------------this is tearDown-------------------')
        # 无需清理环境
        LOG.info('---Opengauss_Function_Tools_pg_config_Case0006执行完成---')
